<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\CommonModel;
use App\Models\HomeModel;
use Illuminate\Support\Facades\DB;
use Storage;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;
use App\Models\AdvertModel;
use App\Models\AdvertThumpModel;

// require_once '../app/libary/captcha/Code.class.php';

class IndexController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(){
        // $global = HomeModel::getGlobalVal();
        // $menu = HomeModel::getMenu();
        // view()->share(['global'=> $global,'menu'=>$menu]);
    }
    public function index(){
        
        // 首頁banner圖
        $banner = AdvertModel::find(1);        
        $banchard = AdvertThumpModel::where(['ad_id'=>$banner->id])->get();
        $newTime = time();

        $golds = DB::table('gold_price')->select(['id','name','buy_price','sale_price','type'])->where(['is_show'=>1])->orderBy('sort_order','desc')->get();
        $gold = CommonModel::getOne('gold_price',['type'=>1]);
        $type_id = 1;
        $article = CommonModel::getOne('article',['type_id'=>$type_id]);
        $news1 = DB::table('article')->select('id','title','type_id')->where(['is_hidden'=>0,'type_id'=>7])->limit(7)->orderBy('sort_order','desc')->orderBy('id','desc')->get();
        $news2 = DB::table('article')->select('id','title','type_id')->where(['is_hidden'=>0,'type_id'=>8])->limit(7)->orderBy('sort_order','desc')->orderBy('id','desc')->get();
        // dd($news1);
        return view('home/index/index',['type_id'=>$type_id,'article'=>$article,'news1'=>$news1,'news2'=>$news2,'banner'=>$banner,'banchard'=>$banchard,'gold'=>$gold,'golds'=>$golds,'newTime'=>$newTime]);
    }
    public function message(){
        $data = Session::all();
        $global = HomeModel::getGlobalVal();
        return view('home/public/message',['global'=> $global,'msg'=>$data['msg'],'url'=>$data['url'],'res'=>$data['res']]);
    }

    public function product($pro_type=0){
        $type_id = 2;
        $count = 12; // 每頁顯示產品數量
        if(isset($_POST['keywords'])){
            $keywords = trim($_POST['keywords']);
            $list = DB::table('product')->select('id','name','price')->where('is_show','=',1)->where('name','like','%'.$keywords.'%')->orderBy('sort_order','desc')->paginate($count);
        }elseif($pro_type){
            $cate_arr = CommonModel::getField('product_category','id');
            // dd($cate_arr);
            // if(isset($cate_arr[$pro_type-1])){
            if($cate_arr[$pro_type-1]){
                $list = HomeModel::getProducts(['id','name','price'],['type_id'=>$pro_type,'is_show'=>1],$count);
            }else{
                $pro_type = 0;
                $list = HomeModel::getProducts(['id','name','price'],['is_show'=>1],$count);
            }
        }else{
            $pro_type = 0;
            $list = HomeModel::getProducts(['id','name','price'],['is_show'=>1],$count);
        }
        // dd($list);
        foreach($list as $key => $value) {
            $product_img = HomeModel::getImage(['id','img_url'],['pid'=>$value->id,'status'=>0]);
            // dd($product_img);
            if($product_img){
                $list[$key]->img = $product_img->img_url;
            }else{
                $list[$key]->img = '/uploads/product/pro-img.jpg';
            }
        }
        // dd($list);
        $pro_cate = HomeModel::getProductCate();
        if(isset($_POST['keywords'])){
            return view('home/index/product',['type_id'=>$type_id,'list'=>$list,'pro_cate'=>$pro_cate,'pro_type'=>$pro_type,'keywords'=>$keywords]);    
        }else{
            return view('home/index/product',['type_id'=>$type_id,'list'=>$list,'pro_cate'=>$pro_cate,'pro_type'=>$pro_type]);
        }
        
    }

    public function recovery(){
        $type_id = 3;
        return view('home/index/recovery',['type_id'=>$type_id]);
    }

    public function question(){
        $type_id = 4;
        $art_list = CommonModel::getList('article',['id','title','art_content'],['type_id'=>$type_id]);
        return view('home/index/question',['type_id'=>$type_id,'art_list'=>$art_list]);
    }
    public function about(){
        $type_id = 5;
        $art_list = CommonModel::getList('article',['id','title','art_keyword','art_content'],['type_id'=>$type_id]);
        // dd($art_list);
        return view('home/index/about',['type_id'=>$type_id,'art_list'=>$art_list]);
    }
    // 新聞列表
    public function news(){
        $type_id = 1;
        $count = 15;
        // DB::enableQueryLog();
        $news = DB::table(DB::raw('zzb_article a'))->leftJoin(DB::raw('zzb_article_category c'), DB::raw('c.id'), '=', DB::raw('a.type_id'))->select(DB::raw('a.id,a.title,a.art_time,a.type_id,c.type_name'))->whereRaw('a.is_hidden=0 and (a.type_id =7 or a.type_id=8)')->orderBy(DB::raw('a.sort_order'),'desc')->orderBy(DB::raw('a.id'),'desc')->paginate($count);
        // $queries = DB::getQueryLog();
        // dd($queries);
        // dd($news);
        return view('home/index/news',['type_id'=>$type_id,'news'=>$news]);
    }
    // 新聞內容頁
    public function news_detail($id){
        $type_id = 1;
        $article = CommonModel::getOne('article',['id'=>$id],['id','title','art_writer','art_time','art_content','type_id']);
        $art_cate = CommonModel::getOne('article_category',['id'=>$article->type_id],['id','type_name','type_url','pid']);
        // dd($article);
        return view('home/index/news_detail',['type_id'=>$type_id,'article'=>$article,'art_cate'=>$art_cate]);
    }


}
